//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;

namespace OpenLS.UI.Themes
{
    partial class GalleryResources
    {
        public GalleryResources()
        {
            InitializeComponent();
        }
        void onItemMouseDown(object sender, MouseEventArgs args)
        {
            ContentControl item = (ContentControl) sender;
            Gallery gallery =  (Gallery) (ItemsControl.ItemsControlFromItemContainer(item));

            gallery.SelectedItem = item.Content;
            args.Handled = true;
        }
        private void onDragDelta(object sender, DragDeltaEventArgs e)
        {
            var element = (FrameworkElement) sender;
            var popup = (Popup) element.Tag;
            {
                double h = popup.Height + e.VerticalChange;
                h = Math.Max(h, element.ActualHeight);
                popup.Height = h;


            }

            {

                double w = popup.Width + e.HorizontalChange;
                w = Math.Max((w), 0);
                w = Math.Max((w), element.MinWidth);
                popup.Width = w;
            }
        }


        private void onLoaded(object sender, RoutedEventArgs e)
        {
            var b = (FrameworkElement)sender;
            var parent = (Gallery)b.TemplatedParent;
            if (GalleryElement.GetLastSelected(parent) != null)
            {
                var container = parent.ItemContainerGenerator.ContainerFromItem(parent.SelectedItem) as IInputElement;
                if (container != null && parent.IsOpen)
                    container.Focus();
            }
            parent.ScrollIntoView(parent.SelectedItem);

        }
        private void changeIsOpen(object sender, bool value)
        {
            var b = (FrameworkElement)sender;
            var parent = (Gallery)b.TemplatedParent;

            if (parent == null)
                return;
            if (true)
                GalleryElement.SetLastSelected(parent, parent.SelectedItem);
            else
            {
                GalleryElement.SetLastSelected(parent, null);
            }
            parent.IsOpen = value;
            //GalleryElement.SetIsOpen(parent, value);
        }

       
        private void onOpen2(object sender, ExecutedRoutedEventArgs e)
        {
            changeIsOpen(sender, true);
        }
        void onOpen3(object sender, ExecutedRoutedEventArgs e)
        {
            changeIsOpen(sender, true);
            FrameworkElement element = (FrameworkElement) sender;
            Control templated = (Control) element.TemplatedParent;
            Popup popup = (Popup) templated.Template.FindName("PART_Popup", templated);
            popup.SetBinding(Popup.IsOpenProperty,
                             new Binding("IsOpen")
                                 {RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent)});
           // popup.IsOpen = true;

        }
        private void popup_Closed(object sender, EventArgs e)
        {
            changeIsOpen(sender, false);
        }

        private void onPopupKeyDown(object sender, KeyEventArgs e)
        {
            var popup = (Popup)sender;
            Gallery gallery = popup.TemplatedParent as Gallery;
            if (gallery == null)
                return;
            switch (e.Key)
            {
                case Key.Escape:
                    gallery.IsOpen = false;
                    TraversalRequest  request = new TraversalRequest(FocusNavigationDirection.Previous);
                    gallery.MoveFocus(request);
                    break;
                case Key.Enter:
                    var lb = (Gallery)popup.TemplatedParent;
                    var lbi = Keyboard.FocusedElement as GalleryItem;
                    if (lbi != null)
                    {
                        lb.SelectedItem = lbi.Content;
                        GalleryElement.SetLastSelected(lb, lbi.Content);
                    }
                    gallery.IsOpen = false;
                    break;
            }
        }


        private void onOpenedLoaded(object sender, RoutedEventArgs e)
        {
            onLoaded(sender, e);
            return;
        }

        private void Grid_MouseDown(object sender, MouseButtonEventArgs e)
        {
            changeIsOpen(sender, true);
            FrameworkElement element = (FrameworkElement)sender;
            Control templated = (Control)element.TemplatedParent;
            Popup popup = (Popup)templated.Template.FindName("PART_Popup", templated);
            popup.SetBinding(Popup.IsOpenProperty,
                             new Binding("IsOpen") { RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent) });
            // popup.IsOpen = true;
        }



    }
}
